package com.ruantu.clothing.service.impl;

import com.ruantu.clothing.dao.JgMapper;
import com.ruantu.clothing.dao.PifashangMapper;
import com.ruantu.clothing.dao.VipMapper;
import com.ruantu.clothing.dao.ZhekoucelueMapper;
import com.ruantu.clothing.entity.*;
import com.ruantu.clothing.service.PifashangService;
import com.ruantu.clothing.util.MyException;
import com.ruantu.clothing.util.UUIDUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/6/13 0013.
 */
@Service
public class PifashangServiceImpl implements PifashangService {

    @Autowired
    private PifashangMapper pifashangMapper;
    @Autowired
    private ZhekoucelueMapper zhekoucelueMapper;
    @Autowired
    private JgMapper jgMapper;
    @Autowired
    private VipMapper vipMapper;

    @Override
    public List<Pifashang> findList(Pifashang pifashang, PageBean pageBean) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(pifashang.getKehuming())) {
            map.put("kehuming", pifashang.getKehuming());
        }
        if (StringUtils.isNotBlank(pifashang.getShengfen()) && !"-1".equals(pifashang.getShengfen())) {
            map.put("shengfen", pifashang.getShengfen());
        }
        if (pifashang.getJgid() != null) {
            //机构类型为分公司的查询总部的
            if (pifashang.getLeixing() == 1) {
                map.put("jgId", 1);
            } else {
                map.put("jgId", pifashang.getJgid());
            }
        }
        map.put("start", pageBean.getStart());
        map.put("rows", pageBean.getRows());
        pageBean.setTotal(pifashangMapper.findListCount(map));
        return pifashangMapper.findList(map);
    }

    @Override
    public XSSFWorkbook downExcel(Pifashang pifashang) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(pifashang.getKehuming())) {
            map.put("kehuming", pifashang.getKehuming());
        }
        if (StringUtils.isNotBlank(pifashang.getShengfen()) && !"-1".equals(pifashang.getShengfen())) {
            map.put("shengfen", pifashang.getShengfen());
        }
        if (pifashang.getJgid() != null) {
            //机构类型为分公司的查询总部的
            if (pifashang.getLeixing() == 1) {
                map.put("jgId", 1);
            } else {
                map.put("jgId", pifashang.getJgid());
            }
        }
        List<Pifashang> list = pifashangMapper.findList(map);
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet("sheet1");
        CellStyle titleStyle = wb.createCellStyle();
        titleStyle.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index);
        titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        XSSFFont font = wb.createFont();
        font.setColor(IndexedColors.WHITE.getIndex());
        titleStyle.setFont(font);
        // 设置这些样式
        int count = 0;
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue("客户名称");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("联系方式");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("归属地");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("店名");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("收货地址");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("授信额度");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("默认折扣");
        cell.setCellStyle(titleStyle);
        CellStyle titleStyle2 = wb.createCellStyle();
        titleStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        titleStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        for (int i = 0; i < list.size(); i++) {
            count = 0;
            row = sheet.createRow(i + 1);
            //客户名称
            cell = row.createCell(count);
            cell.setCellValue(list.get(i).getKehuming());
            cell.setCellStyle(titleStyle2);
            //联系方式
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getPhone());
            cell.setCellStyle(titleStyle2);
            //归属地
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getShengfen());
            cell.setCellStyle(titleStyle2);
            //店名
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getDianming());
            cell.setCellStyle(titleStyle2);
            //收货地址
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getAddress());
            cell.setCellStyle(titleStyle2);
            //授信额度
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getShouxinedu());
            cell.setCellStyle(titleStyle2);
            //默认折扣
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getPifaZhekou());
            cell.setCellStyle(titleStyle2);
        }
        for (int i = 0; i < count; i++) {
            sheet.setColumnWidth(i, 4000);
        }
        return wb;
    }

    @Override
    public int delPifashang(Pifashang pifashang) {
        int code = pifashangMapper.updateDeLFlag(pifashang);
        if (code <= 0) {
            throw new MyException("删除失败，请确认信息后重新尝试");
        }
        return code;
    }

    @Override
    @Transactional
    public int savePifashang(Pifashang pifashang) {
        Zhekoucelue zhekoucelue = new Zhekoucelue();
        zhekoucelue.setId(UUIDUtil.getUUID());
        zhekoucelue.setJgId(pifashang.getJgid());
        zhekoucelue.setPifaZhekou(pifashang.getPifaZhekou());
        //修改
        if (StringUtils.isNotBlank(pifashang.getId())) {
            int code = pifashangMapper.findPifashangByName(pifashang);
            if (code > 0) {
                throw new MyException("修改失败，已存在相同的批发商");
            }
            int codes = pifashangMapper.updateByPrimaryKeySelective(pifashang);
            if (codes <= 0) {
                throw new MyException("修改失败，请确认信息重新尝试");
            }

            //查询是否存在该折扣  没有增加  有更新
            zhekoucelue.setKehuid(pifashang.getId());
            int count = zhekoucelueMapper.findCountByJgidAndKehuId(zhekoucelue);
            //存在更新

            if (count > 0) {
                zhekoucelueMapper.updateZhekou(zhekoucelue);
            } else {
                zhekoucelueMapper.insertSelective(zhekoucelue);
            }
            return codes;
        } else {
            //新增
            //查询是否存在该批发商
            int count = pifashangMapper.findPifashangByName(pifashang);
            if (count > 0) {
                throw new MyException("新增失败，已存在相同的批发商");
            }
            pifashang.setId(UUIDUtil.getUUID());
            int code = pifashangMapper.insertSelective(pifashang);
            if (code <= 0) {
                throw new MyException("新增失败，请检查数据重复");
            }
            zhekoucelue.setKehuid(pifashang.getId());
            //增加折扣策略表
            code = zhekoucelueMapper.insertSelective(zhekoucelue);
            if (code <= 0) {
                throw new MyException("保存失败，请检查数据重新尝试");
            }
            return code;
        }
    }

    @Override
    public int findPifashang(Pifashang pifashang) {
        int code = pifashangMapper.findPifashangByName(pifashang);
        if (code > 0) {
            throw new MyException("该名称已存在");
        }
        return code;
    }

    @Override
    public Pifashang findPifashangById(Pifashang pifashang) {
        Pifashang pifashangs = pifashangMapper.findPifashangById(pifashang);
        if (pifashangs == null) {
            throw new MyException("修改数据不存在，请确认信息重试");
        }
        return pifashangs;
    }


    /***
     *获得所有批发商
     * @param pfs
     * @return
     */
    @Override
    public List<Pifashang> getAllLIstPFS(Pifashang pfs) {
        Map<String, Object> mapCan = new HashMap<>();
        mapCan.put("jgid", pfs.getJgid());
        mapCan.put("delFlag", pfs.getDelFlag());
        return pifashangMapper.findAllPFS(mapCan);
    }

    @Override
    public List<Pifashang> selectAllPFSForJGLX(User u) {

        Map<String, Object> mapCan = new HashMap<>();
        mapCan.put("jgid", u.getJgid());
        mapCan.put("jglx", u.getJgleixing());
        mapCan.put("shengfen", u.getShengfen());
        mapCan.put("delFlag", 0);
        return pifashangMapper.selectAllPFSForJGLX(mapCan);
    }

    /***
     * 退货获得客户
     * @param u
     * @return
     */
    @Override
    public List<Pifashang> selectallKeHu(User u) {
        Map<String, Object> mapCan = new HashMap<>();
        mapCan.put("jgId", u.getJgid());
        mapCan.put("jglx", u.getJgleixing());
        mapCan.put("parenId", u.getParentId());
        mapCan.put("shengfen", u.getShengfen());
        mapCan.put("delFlag", 0);
        List<Pifashang> JGList = jgMapper.selectJGforLeiXing(mapCan);
        List<Pifashang> pfsList = pifashangMapper.selectAllPFSForJGLX(mapCan);
        pfsList.addAll(JGList);
        return pfsList;
    }


    /***
     * 获得客户：报表用
     * @param u
     * @return
     */
    @Override
    public List<Pifashang> selectallKeHureport(User u) {
        Map<String, Object> mapCan = new HashMap<>();
        mapCan.put("jgId", u.getJgid());
        mapCan.put("jglx", u.getJgleixing());
        mapCan.put("parenId", u.getParentId());
        mapCan.put("shengfen", u.getShengfen());
        List<Pifashang> vipList = vipMapper.selectVipForJG(mapCan);
        List<Pifashang> JGList = jgMapper.selectJGforLeiXing(mapCan);
        List<Pifashang> pfsList = pifashangMapper.selectAllPFSForJGLX(mapCan);
        pfsList.addAll(JGList);
        pfsList.addAll(vipList);
        return pfsList;
    }


}
